//
//  ResultSet.m
//  iModern
//
//  Created by Ralf on 5/13/15.
//  Copyright (c) 2015 com.starlooking. All rights reserved.
//

#import "ResultSet.h"

@implementation ResultSet

@synthesize data;
@synthesize statusCode;
@synthesize errorCode;
@synthesize errorMessage;

- (id)initWithParameters:(id)_data statusCode:(NSInteger)_statusCode errorCode:(NSInteger)_errorCode errorMessage:(NSString *)_errorMessage
{
    self.data         = _data;
    self.statusCode   = _statusCode;
    self.errorCode    = _errorCode;
    self.errorMessage = _errorMessage;

    return [super init];
}

- (BOOL)isEmpty
{
    if (self.data == nil || [self.data isEqual:[NSNull null]])
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

- (BOOL)hasErrors
{
    if (statusCode == 200)
    {
        return NO;
    }
    else
    {
        return YES;
    }
}

- (NSArray *)toArray
{
    if ([self isEmpty] || [self hasErrors])
    {
        return nil;
    }

    return data;
}

- (NSMutableArray *)toMutableArray
{
    NSArray *array = [self toArray];
    if (array)
    {
        NSMutableArray *mutableArray = [NSMutableArray new];

        for (NSDictionary *o in array)
        {
            NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] initWithDictionary:o];
            [mutableArray addObject:mutableDictionary];
        }

        return mutableArray;
    }

    return nil;
}

- (NSDictionary *)toDictionary
{
    return data;
}

- (NSMutableDictionary *)toMutableDictionary;
{
    if ([self isEmpty] || [self hasErrors])
    {
        return nil;
    }

    return [[NSMutableDictionary alloc] initWithDictionary:data];
}

@end
